/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetofinal;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.AbstractCellEditor;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

class ButtonColumn extends AbstractCellEditor  
        implements TableCellRenderer, TableCellEditor, ActionListener{  
    JTable table;  
    JButton renderButton;  
    JButton editButton;  
    String text;  
    ImageIcon iconImage;
    Action action;

    public ButtonColumn(JTable table, Action action, int column, URL iconUrl)  
    {  
        super();  
        this.table = table;  
        iconImage = new ImageIcon(iconUrl);
        renderButton = new JButton(iconImage);  
        this.action = action;
        
        editButton = new JButton();  
        editButton.setFocusPainted( false );  
        editButton.addActionListener( this );  

        TableColumnModel columnModel = table.getColumnModel();  
        columnModel.getColumn(column).setCellRenderer( this );  
        columnModel.getColumn(column).setCellEditor( this );  
    }  

    @Override
    public Component getTableCellRendererComponent(  
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)  
    {  
        if (hasFocus)  
        {  
            renderButton.setForeground(table.getForeground());  
            renderButton.setBackground(UIManager.getColor("Button.background"));  
        }  
        else if (isSelected)  
        {  
            renderButton.setForeground(table.getSelectionForeground());  
             renderButton.setBackground(table.getSelectionBackground());  
        }  
        else  
        {  
            renderButton.setForeground(table.getForeground());  
            renderButton.setBackground(UIManager.getColor("Button.background"));  
        }  

        renderButton.setText( (value == null) ? "" : value.toString() );  
        return renderButton;  
    }  

    @Override
    public Component getTableCellEditorComponent(  
        JTable table, Object value, boolean isSelected, int row, int column)  
    {  
        //text = (value == null) ? "" : value.toString();  
        //editButton.setText( text );  
        return editButton;  
    }  

    @Override
    public Object getCellEditorValue()  
    {  
        return text;  
    }  

    @Override
    public void actionPerformed(ActionEvent e)
	{
		int row = table.convertRowIndexToModel( table.getEditingRow() );
		fireEditingStopped();

		//  Invoke the Action

		ActionEvent event = new ActionEvent(
			table,
			ActionEvent.ACTION_PERFORMED,
			"" + row);
		action.actionPerformed(event);
	}  
}
